<?php

$flyResultSet = getFlyById($_GET['fid']);

if ($_GET['page'] == '') {
  $start = 1;
  $page = 1;
} else {
  $start = ($_GET['page'] - 1) * 20 + 1;
  $page = $_GET['page'];
}

$commentsResultSet = getFlyComments($start - 1, $start + 19, $_GET['fid']);

$comments = countFlyComments($_GET['fid']);

$pages = pagesFromMessages($comments);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=<?= getMessageByKey("default.charset") ?>"/>
  <title><?= getMessageByKey("fly.title") ?></title>
  <link rel="stylesheet" type="text/css" href="css/navigation.css">
  <link rel="stylesheet" type="text/css" href="css/menu.css">
  <link rel="stylesheet" type="text/css" href="css/fly.css">
  <link rel="stylesheet" type="text/css" href="css/common.css">
  <link rel="stylesheet" type="text/css" href="css/comment-template.css">
  <link rel="stylesheet" type="text/css" href="css/pages-template.css">
  <script src="resources/jquery.js"></script>

  <script language="JavaScript">
    function reply(i) {
      $('#reply').val(i);
      $('#reply2').val(i);
      $('#textarea').focus();
      $('#textarea').attr("placeholder", "Вы процитировали сообщение, введите ваш ответ.. ")
    }

    function customFormSubmit() {
      $('#maximized_navigation').show();
      $('#commentform').submit();
    }
  </script>
</head>
<body>

<?php
include_once("menu.php");
?>

<div class="maincommon">
  <?php
  if ($fly = mysql_fetch_assoc($flyResultSet)) {
    $flyImagesResultSet = getFlyImages($fly['id'], $fly['mid']);
    ?>
    <div class="flyimagediv">
      <img src="<?= \controller\Config::getPropertyByKey('directory.preview.fly.topic') . $fly['path'] ?>">
    </div>
    <div class="flylegenddiv">
      <div class="header">
        <?= processText($fly['header']) ?>
      </div>
      <div class="content">
        <?= processText($fly['legend']) ?>
      </div>
      <div class="footer">
        <?= getMessageByKey("general.number") ?> <?= $fly['id'] ?><br>
        <?= getMessageByKey("general.date") ?> <?= $fly['date'] ?><br>
        <?= getMessageByKey("general.time") ?> <?= $fly['time'] ?><br>
        <?= getMessageByKey("topic.author") ?> <?= $fly['author'] ?>
      </div>
    </div>
    <div class="flyotherimagesdiv">
      <?php
      while ($flyImage = mysql_fetch_assoc($flyImagesResultSet)) {
        ?>
        <img src="<?= \controller\Config::getPropertyByKey('directory.preview.fly.topic') . $flyImage['path'] ?>"
             onclick="location.href='?action=fullimg&sec=fly&ipath=<?= $flyImage['path'] ?>'">
      <?php
      }
      ?>
    </div>
    <div class="buttonfacebook" onclick="<?= facebookShare($fly['header'], $fly['legend'], $fly['path'])?>">
      Facebook.com
    </div>
  <?php
  }
  ?>

  <?php

  generateFlyPages($pages, $page, $fly['id']);

  while ($commentRow = mysql_fetch_assoc($commentsResultSet)) {
    $paramArray = null;
    $paramArray['comment.id'] = $commentRow['id'];
    $paramArray['comment.date'] = $commentRow['date'];
    $paramArray['comment.time'] = $commentRow['time'];
    $paramArray['user.image'] = \controller\Config::getPropertyByKey('directory.preview.user.image') . $commentRow['img'];
    $paramArray['user.registration'] = $commentRow['registered'];
    $paramArray['user.nick'] = getTextWithAcronym($commentRow['author'], 15);
    $paramArray['user.email'] = getTextWithAcronym($commentRow['email'], 22);
    $paramArray['user.messages'] = $commentRow['msg'];


    if ($commentRow['quote'] != 0) {
      $quoteRow = mysql_fetch_assoc(getFlyComment($commentRow['quote']));
      $quoteRow['content'];
      $paramArray['quote.author'] = $quoteRow['author'];
      $paramArray['quote'] = $quoteRow['content'];
    }
    $paramArray['content'] = processText($commentRow['content']);

    $paramArray['button.like']['link'] = "";
    if (!isLiked("c", $commentRow['id'])) {
      $paramArray['button.like']['link'] = "location.href = '?action=likeflycomment&fid=" . $commentRow['id'] . "&like=1'";
    }
    $paramArray['button.like']['text'] = getMessageByKey("button.user.comment.likes") . "(" . $commentRow['likes'] . ")";

    $paramArray['button.dislike']['link'] = "";
    if (!isDisliked("c", $commentRow['id'])) {
      $paramArray['button.dislike']['link'] = "location.href = '?action=likeflycomment&fid=" . $commentRow['id'] . "&dislike=1'";
    }
    $paramArray['button.dislike']['text'] = getMessageByKey("button.user.comment.dislikes") . "(" .
        $commentRow['dislikes'] . ")";

    $isSpammed = isSpammed("c", $commentRow['id']);

    $paramArray['button.spam']['link'] = "location.href = '?action=spamflycomment&fid=" . $commentRow['id'] . "'";
    $paramArray['button.spam']['text'] = getMessageByKey("button.user.comment.spam");

    $paramArray['button.reply']['link'] = "reply('" . $commentRow['id'] . "')";
    $paramArray['button.reply']['text'] = getMessageByKey("button.user.comment.reply");

    $paramArray['button.profile']['link'] = "location.href='?action=profile&uid=" . $commentRow['uid'] . "'";
    $paramArray['button.profile']['text'] = getMessageByKey("button.comment.user.profile");

    $paramArray['button.message']['link'] = "location.href='?action=convch&uid=" . $commentRow['uid'] . "'";
    $paramArray['button.message']['text'] = getMessageByKey("button.comment.user.message");

    $paramArray['user.id'] = $commentRow['id'];

    $isOwner = false;
    if ($_SESSION['id'] == $commentRow['uid']) {
      $isOwner = true;
    }

    echo generateFlyComment($paramArray, $isOwner, $isSpammed, init());
    ?>

  <?php
  }

  if (init()) {
    ?>
    <form enctype="multipart/form-data" accept-charset="UTF-8" action="?action=flycomhand" method="post"
          id="commentform">
      <a id="anchor">

        <div class="newcomment">
          <div class="info">
            <?= getMessageByKey("user.comment.new") ?>
          </div>
          <textarea name="content" id="textarea" rows="11"
                    placeholder="<?= getMessageByKey("user.comment.new.placeholder") ?>"></textarea>

          <input type="hidden" name="fid" value="<?= $_GET['fid'] ?>">
          <input type="hidden" name="reply" id="reply">

          <div class="buttonsdiv">
            <div class="button" onclick="customFormSubmit()">
              <?= getMessageByKey("button.general.send") ?>
            </div>
          </div>
        </div>

      </a>
    </form>
  <?php
  }
  ?>
</div>

<?php
include_once("navigation.php");
?>

</body>
</html>